package com.rules.admin.controller.decision;


import com.google.common.collect.Maps;
import com.rules.admin.entity.DecisionTree;
import com.rules.admin.entity.TreeNode;
import com.rules.admin.entity.TreeDedution;
import com.rules.admin.service.DecisionTreeService;
import com.rules.admin.service.TreeDedutionService;
import com.rules.admin.service.TreeNodeService;
import com.rules.admin.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping({"/decisionTrees"})
public class DecisionTreeController {
   @Autowired
   private DecisionTreeService decisionTreeService;
   @Autowired
   private TreeNodeService treeNodeService;
   @Autowired
   private TreeDedutionService treeDedutionService;

   @RequestMapping(
      method = {RequestMethod.POST}
   )
   @ResponseBody
   public Map<String, Object> createDecisionTree(@RequestBody DecisionTree decisionTree) {
      this.decisionTreeService.create(decisionTree);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.DELETE}
   )
   @ResponseBody
   public Map<String, Object> deleteDecisionTree(@PathVariable Long id) {
      this.decisionTreeService.deleteDecisionTree(id);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.PUT}
   )
   @ResponseBody
   public Map<String, Object> updateDecisionTree(@RequestBody DecisionTree decisionTree) {
      this.decisionTreeService.updateById(decisionTree);
      return ResultUtils.getSuccessResultData();
   }

   @RequestMapping(
      value = {"/{id}"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> getDecisionTree(@PathVariable long id) {
      DecisionTree decisionTree = (DecisionTree)this.decisionTreeService.getById(id);
      return ResultUtils.getSuccessResultData(decisionTree);
   }

   @RequestMapping(
      value = {"/loadDecision"},
      method = {RequestMethod.GET}
   )
   @ResponseBody
   public Map<String, Object> loadDecision(@RequestParam long treeId) {
      Map<String, Object> data = Maps.newHashMap();
      List<TreeNode> treeNodes = this.treeNodeService.getTreeNodesByTreeId(treeId);
      List<TreeDedution> treeDedutions = this.treeDedutionService.getTreeNodesByTreeId(treeId);
      data.put("treeNodes", treeNodes);
      data.put("treeDedutions", treeDedutions);
      return ResultUtils.getSuccessResultData(data);
   }
}
